package cool.taomu.toolkit.micrometer.controller

import cool.taomu.toolkit.micrometer.entity.MetricsDto
import cool.taomu.toolkit.micrometer.service.MeterRegistryService
import io.swagger.annotations.Api
import java.util.Map
import javax.inject.Inject
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses

@Path("metrics")
@Api(tags="获取JVM运行信息")
class MicrometerController {

    @Inject
    MeterRegistryService meterRegistry;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value="运行信息")
    @ApiResponses(#[
        @ApiResponse(code=200, message="获取运行信息成功", response=MetricsDto)
    ])
    def Response metrics() {
        var MetricsDto dto = new MetricsDto();
        var map = meterRegistry.metrics;
        dto.setJvmMemoryUsedValue(getDouble(map, "jvm.memory.used_value"));
        dto.setJvmMemoryCommittedValue(getDouble(map, "jvm.memory.committed_value"));
        dto.setJvmMemoryMaxValue(getDouble(map, "jvm.memory.max_value"));
        dto.setJvmThreadsLiveValue(getDouble(map, "jvm.threads.live_value"));
        dto.setJvmThreadsDaemonValue(getDouble(map, "jvm.threads.daemon_value"));
        dto.setJvmThreadsPeakValue(getDouble(map, "jvm.threads.peak_value"));
        dto.setJvmClassesLoadedValue(getDouble(map, "jvm.classes.loaded_value"));
        dto.setJvmClassesUnloadedCount(getDouble(map, "jvm.classes.unloaded_count"));
        dto.setJvmCompilationTimeCount(getDouble(map, "jvm.compilation.time_count"));
        dto.setJvmGcMemoryAllocatedCount(getDouble(map, "jvm.gc.memory.allocated_count"));
        dto.setJvmGcMemoryPromotedCount(getDouble(map, "jvm.gc.memory.promoted_count"));
        dto.setJvmGcOverheadValue(getDouble(map, "jvm.gc.overhead_value"));
        dto.setJvmGcMaxDataSizeValue(getDouble(map, "jvm.gc.max.data.size_value"));
        dto.setJvmMemoryUsageAfterGcValue(getDouble(map, "jvm.memory.usage.after.gc_value"));
        dto.setJvmGcLiveDataSizeValue(getDouble(map, "jvm.gc.live.data.size_value"));
        dto.setJvmBufferCountValue(getDouble(map, "jvm.buffer.count_value"));
        dto.setJvmBufferTotalCapacityValue(getDouble(map, "jvm.buffer.total.capacity_value"));
        dto.setJvmBufferMemoryUsedValue(getDouble(map, "jvm.buffer.memory.used_value"));
        dto.setJvmThreadsDeadlockedValue(getDouble(map, "jvm.threads.deadlocked_value"));
        dto.setJvmThreadsDeadlockedMonitorValue(getDouble(map, "jvm.threads.deadlocked.monitor_value"));
        dto.setJvmThreadsStatesValue(getDouble(map, "jvm.threads.states_value"));
        dto.setJvmThreadsStartedCount(getDouble(map, "jvm.threads.started_count"));
        dto.setSystemCpuCountValue(getDouble(map, "system.cpu.count_value"));
        dto.setSystemLoadAverage1mValue(getDouble(map, "system.load.average.1m_value"));
        dto.setProcessCpuUsageValue(getDouble(map, "process.cpu.usage_value"));
        dto.setProcessCpuTimeCount(getDouble(map, "process.cpu.time_count"));
        dto.setSystemCpuUsageValue(getDouble(map, "system.cpu.usage_value"));
        return Response.ok(dto).build();
    }

    def static double getDouble(Map<String, Object> map, String key) {
        var Object value = map.get(key);
        if (value === null) {
            return 0.0;
        }
        if (value instanceof Number) {
            return (value as Number).doubleValue();
        }
        try {
            return Double.parseDouble(value.toString());
        } catch (NumberFormatException e) {
            return 0.0;
        }
    }
}
